﻿#include "contact_person_modify_dialog.h"
#include "ui_contact_person_modify_dialog.h"

ContactPersonModifyDialog::ContactPersonModifyDialog(QWidget *parent, ContactPerson *person) :
    QDialog(parent),
    ui(new Ui::ContactPersonModifyDialog)
{
    this->person = person;

    initUi();
    initData();
    initUiData();
    initConnect();
}

ContactPersonModifyDialog::~ContactPersonModifyDialog()
{
    delete ui;
}

void ContactPersonModifyDialog::initUi()
{
    ui->setupUi(this);

    age_edit = ui->age_edit;
    name_edit = ui->name_edit;
    phone_edit = ui->phone_edit;
    address_edit = ui->address_edit;
    sex_group_box = ui->sex_group_box;

    modify_button = ui->modify_button;
    cancel_button = ui->cancel_button;
}

void ContactPersonModifyDialog::initUiData()
{
    setFixedSize(280, 300);

    QRegExp exp("[0-9]+$");
    QValidator *validator = new QRegExpValidator(exp);
    // 输入框只能输入数字
    age_edit->setValidator(validator);
    phone_edit->setValidator(validator);

    name_edit->setText(QString::fromStdString(person->getName()));
    age_edit->setText(QString::fromStdString(to_string(person->getAge())));
    phone_edit->setText(QString::fromStdString(to_string(person->getPhone())));
    address_edit->setText(QString::fromStdString(person->getAddress()));

    if (person->getSex() == 0) {
        ui->boy_button->setChecked(true);

    } else if (person->getSex() == 1) {
        ui->girl_button->setChecked(true);

    } else if (person->getSex() == -1) {
        ui->unknow_button->setChecked(true);
    }
}

void ContactPersonModifyDialog::initData()
{

}

void ContactPersonModifyDialog::initConnect()
{
    // 修改联系人
    connect(modify_button, &QPushButton::clicked, [=]() {
        if (judgeInputIsFull()) {
            person = getInputInfo();

            emit inputComplete();
            this->close();

        } else {
            QMessageBox::information(this, "Tips", "Please fill in all the contact person information!");
        }
    });

    // 取消修改联系人
    connect(cancel_button, &QPushButton::clicked, [=]() {
        this->close();
    });
}

ContactPerson *ContactPersonModifyDialog::getContactPerson()
{
    return person;
}

bool ContactPersonModifyDialog::judgeInputIsFull()
{
    if (age_edit->text() == QString::fromLocal8Bit("")) {
        return false;
    }

    if (name_edit->text() == QString::fromLocal8Bit("")) {
        return false;
    }

    if (phone_edit->text() == QString::fromLocal8Bit("")) {
        return false;
    }

    if (address_edit->text() == QString::fromLocal8Bit("")) {
        return false;
    }

    return true;
}

// 获取输入框的信息
ContactPerson *ContactPersonModifyDialog::getInputInfo()
{
    string name = name_edit->text().toUtf8().data();
    int age = atoi(age_edit->text().toUtf8().data());
    int phone = atoi(phone_edit->text().toUtf8().data());
    string address = address_edit->text().toUtf8().data();
    int sex = ui->boy_button->isChecked() ? 0 : ui->girl_button->isChecked() ? 1 : -1;

    person->setName(name);
    person->setSex(sex);
    person->setAge(age);
    person->setPhone(phone);
    person->setAddress(address);

    return person;
}
